<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/26
 * Time: 下午5:49
 */

namespace App\Admin\Controllers;

use App\Busi\Traits\CategoryTrait;
use App\Models\News;
use App\Models\SecQualifyCategory;
use App\Models\SupQualifyCategory;
use App\Models\Tag;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Controllers\Controller;
use Illuminate\Support\MessageBag;

class NewsController extends Controller
{
    use ModelForm,CategoryTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('新闻列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增新闻');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑新闻');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(News::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->title('标题');

            $object= $this;

            $grid->subcategory_id('分类名称')->display(function ($category_id) use($object){
                return $object->getFullCategoryNameByID($category_id);
            });
            $grid->tags("关联标签")->display(function ($tags){
                $tags = array_map(function ($tag) {
                    return "<a class='success' href='tags/".$tag['id']."' target='_blank'>{$tag['name']}</a><br>";
                }, $tags);

                return join('', $tags);
            });
            $grid->key_words('关键字');
            $grid->show_order('显示顺序');
            $grid->view_num('查看次数');
            $grid->flag('是否生效')->display(function($flag){
                if( $flag==1 ) return '有效';
                return '无效';
            });
            $grid->updated_at('更新时间');
            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {

            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
//                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(News::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->text('title','标题');
            $form->text("desc","描述");
            $form->image('poster', '海报')->uniqueName()->removable();
            $form->select('supcategory_id',"一级类目")->options(SupQualifyCategory::pluck("category_name","id"))->load('subcategory_id', '/api/admin_subcategory');
            $form->select("subcategory_id","二级类目")->options(function (){
                $supcategory_id = $this->supcategory_id;
                return SecQualifyCategory::where("parent_cate_id",$supcategory_id)->pluck('category_name','id');
            });
            $form->multipleSelect('tags','关联标签')->options(Tag::all()->pluck('name', 'id'));
            $form->text("key_words","关键字,多个用-隔开");
            $form->text('show_order','展示顺序');
            $form->text('view_num','查看次数');
            $form->select('flag', '是否生效')->options([0=>'否', 1=>'是']);
            $form->editor('content','内容');
            $form->saving(function ($form) {

//                $error = new MessageBag([
//                    'title'   => '存储错误',
//                    'message' => '标题重复,请重新输入标题',
//                ]);
//
//                return back()->with(compact('error'));
            });


        });

    }

}